package collect

import (
	"context"
	"github.com/robfig/cron/v3"
)

// fileCorn 文件定时器
type fileCorn struct {
	cron   *cron.Cron
	deFunc []*Task
	ctx    context.Context
}
type Task struct {
	TimeStr string
	opt     func()
}

func NewFileCorn(val []*Task, ctx context.Context) (*fileCorn, error) {
	var (
		err error
	)
	c := cron.New()
	for _, task := range val {
		if _, err = c.AddFunc(task.TimeStr, task.opt); err != nil {
			return nil, err
		}
	}
	c.Start()
	f := &fileCorn{
		cron:   c,
		deFunc: val,
		ctx:    ctx,
	}
	return f, nil
}
